package com.l024.easyandroidlib.log.printer;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.l024.easyandroidlib.R;
import com.l024.easyandroidlib.log.config.EasyLogConfig;
import com.l024.easyandroidlib.log.log.EasyLogMo;
import com.l024.easyandroidlib.log.log.EasyLogType;
import com.l024.easyandroidlib.log.provider.EassyLogViewPrinterProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wu_ming_zhi_bei
 * @date 2021/1/29 9:25
 * @Notes 日志可视化
 */
public class EasyLogViewPrinter implements EasyLogPrinter {

    private RecyclerView recyclerView;
    private LogAdapter adapter;
    private EassyLogViewPrinterProvider viewProvider;

    /**
     * 初始化布局
     * @param
     */
    public EasyLogViewPrinter(Activity activity) {
        FrameLayout rootView = activity.findViewById(android.R.id.content);
        recyclerView = new RecyclerView(activity);
        adapter = new LogAdapter(LayoutInflater.from(recyclerView.getContext()));
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setAdapter(adapter);
        viewProvider = new EassyLogViewPrinterProvider(rootView,recyclerView);
    }

    @Override
    public void print(@NonNull EasyLogConfig config, int level, String tag, @NonNull String prientString) {
        adapter.addItem(new EasyLogMo(System.currentTimeMillis(),level,tag,prientString));
        //滚动到对应位置
        recyclerView.smoothScrollToPosition(adapter.getItemCount()-1);
    }

    /**
     * 获取EassyLogViewPrinterProvider控制日志view显示隐藏
     * @return
     */
    public EassyLogViewPrinterProvider getViewProvider(){
        return viewProvider;
    }

    private static class LogAdapter extends RecyclerView.Adapter<LogAdapter.LogViewHolder>{
        private LayoutInflater inflater;
        private List<EasyLogMo> logs = new ArrayList<>();
        public LogAdapter(LayoutInflater inflater) {
            this.inflater = inflater;
        }

        //添加日志信息
        void addItem(EasyLogMo easyLogMo){
            logs.add(easyLogMo);
            notifyItemInserted(getItemCount());
        }
        @NonNull
        @Override
        public LogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = inflater.inflate(R.layout.easy_log_item,parent,false);
            return new LogViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull LogViewHolder holder, int position) {
            EasyLogMo easyLogMo = logs.get(position);
            //获取高亮颜色
            int color = getHighlightColor(easyLogMo.level);
            holder.tagView.setTextColor(color);
            holder.messageView.setTextColor(color);

            holder.tagView.setText(easyLogMo.getFlattened());
            holder.messageView.setText(easyLogMo.log);
        }

        @Override
        public int getItemCount() {
            return logs.size();
        }

        /**
         * 根据日志等级显示不同颜色
         */
        private int getHighlightColor(int logLevel){
            int highlight = 0;
            switch (logLevel){
                case EasyLogType.V:{
                    highlight = 0xffbbbbbb;
                    break;
                }
                case EasyLogType.D:{
                    highlight = 0xffffffff;
                    break;
                }
                case EasyLogType.I:{
                    highlight = 0xff6a8759;
                    break;
                }
                case EasyLogType.W:{
                    highlight = 0xffbbb529;
                    break;
                }
                case EasyLogType.E:{
                    highlight = 0xffff6b68;
                    break;
                }
                default:{
                    highlight = 0xffffff00;
                    break;
                }
            }
            return highlight;
        }
        private static class LogViewHolder extends RecyclerView.ViewHolder{
            TextView tagView;
            TextView messageView;
            public LogViewHolder(@NonNull View itemView) {
                super(itemView);
                tagView = itemView.findViewById(R.id.tag);
                messageView = itemView.findViewById(R.id.message);
            }
        }
    }
}
